package com.mr.base.action;

import java.io.IOException;
import java.util.List;

import com.mr.base.domain.Card;
import com.mr.base.service.CardService;
import com.skysz.framework.action.AbstractBaseAction;
import com.skysz.framework.action.ActionResponse;
import com.skysz.framework.action.ResponseType;
import com.skysz.framework.page.PageHandler;
import com.skysz.framework.plugin.dhtmlx.grid.Column;
import com.skysz.framework.plugin.dhtmlx.grid.formatter.StringColumnFomatter;
import com.skysz.framework.plugin.dhtmlx.grid.json.ArrayJsonGrid;

/**
 * 卡管理
 * @author Administrator
 */
public class CardAction extends AbstractBaseAction {
	private static final long serialVersionUID = 1L;
	
	private CardService bcardService;

	public void setBcardService(CardService bcardService) {
		this.bcardService = bcardService;
	}
	
	/**
	 * 查询
	 * @return
	 * @throws IOException
	 */
	public String findAll() throws IOException {
		StringBuffer hql = new StringBuffer();
		hql.append("select t.id, t.type,t.type,t.name,t.initPrice,t.freePrice,t.enableDays, ");
		hql.append("t.status from Bcard t where t.enable=true  ");
		
		
		ArrayJsonGrid grid = new ArrayJsonGrid();
		grid.setIsNeedIndex(true);
		PageHandler ph = new PageHandler(grid);
		ph.setHql(hql.toString());
		ph.addPartHql(" and t.name like '%'||?||'%' ","name");
		ph.addPartHql(" and t.type=? ","type");
		ph.addPartHql(" order by t.id desc");
		grid.addColumn(1, new Column("t.type",new StringColumnFomatter("1,2,3,4,5","非持卡顾客,储值卡,项目卡,局部卡,积分折扣卡")));
		ph.load();
		ph.output();
		return null;
	}
	
	/**
	 * 新增卡
	 * @return
	 * @throws Exception
	 */
	public ActionResponse insert() throws Exception {
		ActionResponse ar = new ActionResponse(ResponseType.FAILURE);
		Card t = super.creatBean(Card.class, true);
		t.setOrg(this.getCurrentOrg()) ;
		super.setWho(t);
		t.setEnable(true);
		
		try {
			List<String[]> list = this.getGrid("grid");
			if (list != null && list.size() >= 1) {
				t.setItemLineList(list);
			}
		} catch(Exception e) {}
		this.bcardService.insertBcard(t);
		//保存成功
		ar.setResponseType(ResponseType.SUCCESS_SAVE);
		return ar;
	}
	
	/**
	 * 修改卡
	 * @return
	 * @throws Exception
	 */
	public ActionResponse update() throws Exception {
		ActionResponse ar = new ActionResponse(ResponseType.FAILURE) ;
		Card t = super.creatBean(Card.class, true) ;
		t.setOrg(this.getCurrentOrg()) ;
		super.setWho(t);
		t.setEnable(true);
		
		try {
			List<String[]> list = this.getGrid("grid");
			if (list != null && list.size() >= 1) {
				t.setItemLineList(list);
			}
		} catch(Exception e) {}
		this.bcardService.updateBcard(t);
		//保存成功
		ar.setResponseType(ResponseType.SUCCESS_SAVE);
		return ar;
	}
	
	/**
	 * 加载卡信息
	 * @param id
	 * @return
	 */
	public Card loadBcard() throws Exception {
		Long cardId = super.getParameter("cardId", Long.class);
		Card b = this.bcardService.loadBcard(cardId);
		return b;
	}
	
	/**
	 * 查找卡行信息
	 * @param id
	 * @param type
	 * @return
	 */
	public String findCardLine() {
		Long cardId = super.getParameter("cardId", Long.class);
		String type = super.getParameter("type");
		List<Object[]> list = this.bcardService.findCardLine(cardId, type);
		
		ArrayJsonGrid grid = new ArrayJsonGrid();
		grid.setIsNeedIndex(true);
		grid.setData(list);
		this.write(grid.serialize());
		return null;
	}
	
	/**
	 * 禁用
	 * @return
	 * @throws Exception
	 */
	public ActionResponse disableCard() throws Exception {
		ActionResponse ar = new ActionResponse(ResponseType.FAILURE);
		Long id = super.getParameter("cardId", Long.class);
		
		this.bcardService.disableBcard(id);
		//保存成功
		ar.setResponseType(ResponseType.SUCCESS_SAVE);
		return ar;
	}
}
